﻿## kill Main Install if Terminate = $true
if ($env:terminate -eq $true) {exit 0}

if ($env:installWAC -eq $false) {
Write-Host -ForegroundColor Yellow "Skipping : Install WAC"
exit 0
}
$env:mosssetuppath = $env:mosssetuppath.Replace("MOSS", "WAC")

#check to see if WAC has already been installed
if (Test-Path "${env:systemdrive}\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML\serverfilesbox4.xml"){
Write-Host -ForegroundColor Yellow "WAC is already installed, Continuing..." $values.BuildVersion
exit 0
} else {
#Install WAC
}

Write-Host "Installing WAC...."
start-Service  msiserver  | Out-Null

Function InstallServer
{
	Param( $SetupPath, $servername )
	Write-Host "Installing WAC from $SetupPath...";
	
	
	$appconfig = ".\wac\o14-wac-setup.xml"
	Copy-Item $appconfig $env:mosssetuppath
	[xml]$xmldoc = get-Content $appconfig


	$node = $xmldoc.CreateElement("INSTALLLOCATION")
	$node.setAttribute("Value", "C:\Program Files\Microsoft Office Servers") | Out-Null
	$xmldoc.Configuration.AppendChild($node)| Out-Null


	$node = $xmldoc.CreateElement("DATADIR")
	

	$node.setAttribute("Value", "C:\SearchIndexes") | Out-Null
	$xmldoc.Configuration.AppendChild($node)| Out-Null


	$node = $xmldoc.CreateElement("PIDKEY")
	$node.setAttribute("Value", "$env:WACPID")
	$xmldoc.Configuration.AppendChild($node)
	
	$xmldoc.Save("$env:mosssetuppath\o14-wac.setup.xml")

	##END - MSIT-ISEG - Change ##
	
	
	Write-Host $appconfig
	
	echo "Installing APP version..."
	& $SetupPath\setup.exe /config "$env:mosssetuppath\o14-wac.setup.xml" | Out-Host
	


	##END MSIT-ISEG - Change ##

	echo "last exit code is $lastexitcode"
	# check the exit code
	if( $lastexitcode -eq 0 )
	{
		echo "installation succeded."
		#echo "You need to restart computer before run psconfig."
	}
	##MSIT-ISEG - Change ##
	elseif( $lastexitcode -eq 30030 )
	{
		Write-Error "A restart is needed."
		$env:terminate = $true
		return "false";
	}
	else
	{
		write-error "installation failed. return code $lastexitcode";
		return "false";
	}
	
	return "true";
}



# Install MOSS
if( $env:mosssetuppath -ne $null)
{
	$ret = (InstallServer $env:mosssetuppath "MOSS")
	echo $ret
	if( $ret -match "false" ) 
	{
		exit 1;
	}
}
else
{
	Write-Error "moss setup path not specified..."
	exit 1;
}

# Install WAC server
if($env:wacsetuppath -ne $null)
{
	$ret = (Installserver $env:wacsetuppath "WAC")
	echo $ret
	if( $ret -match "false" ) 
	{
		exit 1;
	}
}




exit 0;


###########################################################################
#  
#   End
#
###########################################################################

 